package com.cxh.mchat.bussiness.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxh.mchat.bussiness.entity.Message;
import com.cxh.mchat.bussiness.service.IMessageService;
import com.cxh.mchat.common.constant.StatusConstant;
import com.cxh.mchat.common.utils.SecurityUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxh
 * @since 2021-12-30
 */
@RestController
@RequestMapping("/api/message")
public class MessageController {

    @Resource
    private IMessageService messageService;

    @GetMapping("/query")
    public R<List<Message>> queryList(Page<Message> page, String bussinessNo){

        Assert.hasText(bussinessNo, "对方账号不能为空");
        String userNo = SecurityUtils.getLoginUser().getUserNo();
        QueryWrapper<Message> q = new QueryWrapper<Message>().eq("send_user_no", bussinessNo).eq("received_user_no", userNo);
        q.or().eq("send_user_no", userNo).eq("received_user_no", bussinessNo);
        q.orderByAsc("create_time");
        //IPage<Message> data = messageService.page(page, q);

        //状态改为已读
        Message message = new Message();
        message.setStatus(StatusConstant.TRUE);
        messageService.getBaseMapper().update(message, q);

        return R.ok(messageService.getBaseMapper().selectList(q));
    }
}

